<?php
class StaffController extends Controller {
	
	// 员工帐号管理
	function ManageAction()
	{
		$this->view("staff.manage","item");
	}
	
	// 员工帐号管理
	function StaffListAction(){
		$p["p"] = P("pageIndex",0);
		$p["r"] = P("pageSize",20);
		$agency = P("agency","");
		$p["user"] = P("user","");
		$p["name"] = P("name","");
		$p["sortField"] = P("sortField","cid");
		$p["sortOrder"] = P("sortOrder","asc");
		
		$agencyList = CommonModel::getInstance()->getAgencyAllList();
		$p['agency'] = ArrayTool::ArrayToIDs($agencyList,$agency);
		
		$data = StaffModel::getInstance()->getStaff($p);
		$this->json->toJson(array("total"=>StaffModel::getInstance()->getCount($p),"data"=>$data));
	}
	
	// 员工下拉框
	function getStaffListAction(){
		$p["id"]=G("id","0");
		$this->json->toJson(StaffModel::getInstance()->getStaffList($p));
	}
	
	// 员工(禁用)
	function UserEndAction(){
		$p['sid'] = P("sid","0");
		$p['cid'] = P("cid","0");
		$p['stop'] = 1;
		$rs = StaffModel::getInstance()->UserStatusUpd($p);
		if($rs){
			$this->json->code("OK")->out();
		}else{
			$this->json->code("NG")->out();
		}
	}
	
	// 员工(启用)
	function UserStartAction(){
		$p['sid'] = P("sid","0");
		$p['cid'] = P("cid","0");
		$p['stop'] = 0;
		$rs = StaffModel::getInstance()->UserStatusUpd($p);
		if($rs){
			$this->json->code("OK")->out();
		}else{
			$this->json->code("NG")->out();
		}
	}

    function UserDelAction()
    {
        $p['sid'] = P("sid","0");
        $p['cid'] = P("cid","0");
        $p['stop'] = 0;
        $rs = StaffModel::getInstance()->StaffDel($p);
        if($rs){
            $this->json->code("OK")->out();
        }else{
            $this->json->code("NG")->out();
        }
    }

	// 员工(编辑)
	function UserAddEditAction(){
		//登录帐号
		$rs = array();
		$role3 = array();
		$p["sid"]=G("sid","0");
		$role1=FrameworkModel::getInstance()->getRoleList();
		$role2=FrameworkModel::getInstance()->getRoleBySid($p);
		
		foreach ($role2 as $item){
			$role3[]=$item["role"];
		}
		
		foreach ($role1 as $key=>$item){
			if(in_array($item["id"],$role3)){
				$rs[]=$key;
			}
		}

		$this->JS["role"]=$rs;
		$this->view("staff.add.edit","item");
	}
	
	// 员工(增加)
	function UserAddAction(){
		//登录帐号
		$p['user'] = PA(P("data",array()),"user",""); 
		$p['password'] = md5(PA(P("data",array()),"password","888888"));
		$p['agency'] = PA(P("data",array()),"agency","0");
		$p['position'] = PA(P("data",array()),"position","0");
		$p['description'] = '员工';
		
		//员工信息
		$p['name'] = PA(P("data",array()),"name","");
		$p['sex'] = PA(P("data",array()),"sex","未知");
		$p['mobile_phone'] = PA(P("data",array()),"mobile_phone","");
		$p['tel'] = PA(P("data",array()),"tel","");
		$p['email'] = PA(P("data",array()),"email","");
		$p['remark'] = PA(P("data",array()),"remark","");
		
		//角色
		$role = P("role","");
		$p['role']=explode(",",$role);
		
		if($p['user']!="" && $p['name']!=""){
			if(StaffModel::getInstance()->userExists($p)){
				$this->json->code("MG")->msg("登录帐号已经被使用了，重新输入！")->out();
				return;
			}
			if(StaffModel::getInstance()->staffExists($p)){
				$this->json->code("MG")->msg("员工真实姓名已经被使用了，重新输入！")->out();
				return;
			}
			
			$rs = StaffModel::getInstance()->UserAdd($p);
			if($rs){
				$this->json->code("OK")->out();
				return;
			}else{
				$this->json->code("NG")->out();
				return;
			}
		}else{
			$this->json->code("NG")->out();
			return;
		}
	}
	
	// 员工(修改)
	function UserUpdAction(){
		//登录帐号
		$p['id'] = PA(P("data",array()),"sid","0");
		$p['user'] = PA(P("data",array()),"sid","0");
		$p['cid'] = PA(P("data",array()),"cid","0");
		$p['agency'] = PA(P("data",array()),"agency","0");
		$p['position'] = PA(P("data",array()),"position","0");
		$p['description'] = '员工';
		
		//员工信息
		$p['sex'] = PA(P("data",array()),"sex","未知");
		$p['mobile_phone'] = PA(P("data",array()),"mobile_phone","");
		$p['tel'] = PA(P("data",array()),"tel","");
		$p['email'] = PA(P("data",array()),"email","");
		$p['remark'] = PA(P("data",array()),"remark","");
		
		//角色
		$role = P("role","");
		$p['role']=explode(",",$role);
		
		if($p['id']!="0" && $p['cid']!="0"){			
			$rs = StaffModel::getInstance()->UserUpd($p);
			if($rs){
				$this->json->code("OK")->out();
				return;
			}else{
				$this->json->code("NG")->out();
				return;
			}
		}else{
			$this->json->code("NG")->out();
			return;
		}
	}
	
	//菜单
	function MenuAllAction(){
		$this->json->toJson(FrameworkModel::getInstance()->getMenusAllList());
	}
	
	//菜单
	function RoleAction(){
		$this->json->toJson(FrameworkModel::getInstance()->getRoleList());
	}
	
	// 职位
	function PositionAction(){
		$p["agency"] = G("agency","1");
		$this->json->toJson(StaffModel::getInstance()->StaffPosition($p));
	}
	
	//角色管理
	function RolesAction(){
		$this->view("staff.role","item");
	}
	
	//增加修改角色
	function RoleUpdAction(){
		$p['id'] = G("id","0");
		$p['name'] = PA(P("data",array()),"name","");
		$p['desc'] = PA(P("data",array()),"desc","");
		if($p['id'] == 0){
			StaffModel::getInstance()->RoleAdd($p);
		}else{
			StaffModel::getInstance()->RoleUpd($p);
		}
	}
	
	//删除角色
	function RoleDelAction(){
		$p['id'] = P("id","0");
		if($p['id'] != 0){
			$rs = StaffModel::getInstance()->RoleDel($p);
			if($rs){
				$this->json->code("OK")->out();
			}else{
				$this->json->code("MG")->msg("您要删除的角色已经被使用了，不能删除！")->out();
			}
		}else{
			$this->json->code("NG")->out();
		}
	}
	
	//修改角色权限
	function RoleSaveAction(){
		$p['id'] = P("id","0");
		$p['role'] = P("role","");
		if($p['id'] != 0){
			StaffModel::getInstance()->RoleSave($p);
			$this->json->code("OK")->out();
		}else{
			$this->json->code("NG")->out();
		}
	}
}
?>